package cn.shop.gameapi.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.shop.gameapi.model.GatewaySecret;
import cn.shop.gameapi.model.MerchantUser;
import cn.shop.gameapi.req.LoginReq;
import cn.shop.gameapi.util.IpUtil;
import cn.shop.gameapi.util.ResponseInfo;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static cn.shop.gameapi.model.SqlContent.*;

/**
 * @author : 李一涵
 * @date : Created in 2021-12-7 16:11
 * @description :
 */
@Slf4j
@RestController
public class LoginController {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @PostMapping("/login")
    public ResponseInfo<MerchantUser> login(LoginReq login, HttpServletRequest request) {
        if (StrUtil.isBlank(login.getAccount()) || StrUtil.isBlank(login.getPassword())) {
            return ResponseInfo.failed("用户名和密码不能为空");
        }
        log.info("收到网关登录请求：{}", login);
        String ip = IpUtil.getClientIp(request);
        String secret;
        GatewaySecret gs = null;
        try {
            gs = jdbcTemplate.queryForObject(SELECT_SECRET_IP, new BeanPropertyRowMapper<>(GatewaySecret.class), ip);
        } catch (EmptyResultDataAccessException ignored) {}
        MerchantUser user = jdbcTemplate.queryForObject(SELECT_MERCHANT, new BeanPropertyRowMapper<>(MerchantUser.class), login.getAccount());
        if (user == null) {
            return ResponseInfo.failed("");
        }
        if (!DigestUtil.bcryptCheck(login.getPassword(), user.getPassword())) {
            return ResponseInfo.failed("");
        }
        if (gs == null) {
            gs = new GatewaySecret();
            gs.setIp(ip);
            gs.setId(IdWorker.getId());
            gs.setSecret(IdUtil.simpleUUID().toUpperCase());
            gs.setUid(user.getId());
            user.setSecret(gs.getSecret());
            int count = jdbcTemplate.update(INSERT_SECRET, gs.getId(), gs.getUid(), gs.getIp(), gs.getSecret());
            if (count == 0) {
                return ResponseInfo.failed("");
            }
            user.setSecret(gs.getSecret());
        } else if (!gs.getUid().equals(user.getId())) {
            secret = IdUtil.simpleUUID().toUpperCase();
            int count = jdbcTemplate.update(UPDATE_SECRET, user.getId(), secret, gs.getIp());
            if (count == 0) {
                return ResponseInfo.failed("");
            }
            user.setSecret(secret);
        } else {
            /*if (!StrUtil.equals(gs.getSecret(), secret)) {
                return ResponseInfo.failed("");
            }*/
            user.setSecret(gs.getSecret());
        }
        return ResponseInfo.ok(user);
    }

}
